import { reactive, onMounted, ref, nextTick } from 'vue'
import useInstance from '@/hooks/useInstance';
import { taskDetailApi } from '@/api/ReviewBusiness/01Project/project'
import { auditorUploadFileApi } from '@/api/ReviewBusiness/01Project/project'
import { AuditFileAdd, getdetail } from '@/api/ReviewBusiness/01Project/projectType'
import { EditType } from '@/type/BaseEnum'
import { isFileEdition } from '@/utils/validate'
export default function AuditAddAndEdit(getAuditList?, searchParams?) {

  const { global, proxy } = useInstance()
  const RefAuditAddEdit = ref<{ show: (type: string, row?: any, taskBaselineId?: string) => void }>()
  const isFileEditionPass = (rule, value, callback) => {
    if (isFileEdition(value)) {
      callback()
    } else {
      return callback(new Error('输入正确的版本号'))
    }
  }
  const rules = reactive({
    taskBaseline: [{
      required: true,
      message: '请选择基线...',
      trigger: 'change',
    }],
    id: [{
      required: true,
      message: '请选择阶段...',
      trigger: 'blur',
    }],
    wordFiles: [{
      required: true,
      message: '请上传文件',
      trigger: 'blur',
    }],
  })
  const dialogModel = reactive<AuditFileAdd>({
    auditorStageId: '',
    taskBaselineId: '',
    versions: '',
    wordFiles: '',
    taskId: '',
    filesId: ''
  })
  const addBtn = (fileType) => {
    console.log(fileType);
    RefAuditAddEdit?.value?.show(EditType.ADD, fileType)
  }
  const editBtn = (row, taskBaselineId) => {
    RefAuditAddEdit?.value?.show(EditType.EDIT, row, taskBaselineId)
  }
  // 保存
  const saveFile = async (row) => {
    const arr = { ...row }
    const formData = new FormData();
    arr.wordFiles.forEach((item) => {
      formData.append('wordFiles', item)
    })
    delete arr.wordFiles
    Object.keys(arr).forEach(key => {
      formData.append(key, arr[key]);
    });
    const res = await auditorUploadFileApi(formData)
    if (res.code === 200) {
      global.$message({ message: '上传成功', type: 'success' })
      getAuditList(row.taskId, row.taskBaselineId)
    }
  }
  return {
    addBtn, editBtn, RefAuditAddEdit, saveFile, dialogModel, rules,
  }
}